package seatiger.tomcat.catalina;

import seatiger.tomcat.util.StringManager;

import java.io.IOException;
import java.net.URL;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 部署器，帮助context部署项目
 */
public interface Deployer {

    StringManager sm = StringManager.getManager(Constants.PACKAGE_CORE);

    /**
     * 根据配置文件路径 部署项目
     * @param config
     */
    void install(URL config) throws IOException;

    /**
     * 文件夹部署
     * @param contextPath
     * @param dir
     */
    void install(String contextPath,URL dir) throws IOException;

    /**
     * 验证应用路径是否合法
     */
    default void validateContext(StringManager sm, String contextPath, Function<Context,String> function){
        sm = sm == null ? this.sm : sm;
        if (contextPath == null) {
            throw new IllegalArgumentException(sm.getString("standardHost.pathRequired"));
        } else if(!contextPath.equals("") && !contextPath.startsWith("/")){
            throw new IllegalArgumentException(sm.getString("standardHost.pathFormat", contextPath));
        }

        /*if (predicate.test(this.findDeployedApp(contextPath))){

        }
        consumer.accept(this.findDeployedApp(contextPath));*/

        String str = function.apply(this.findDeployedApp(contextPath));
        if (str != null){
            throw new IllegalStateException(str);
        }
    }

    default void validateContext(String contextPath, Function<Context,String> function){
        validateContext(null,contextPath,function);
    }

    /**
     * 找到所有部署的应用
     */
    String[] findDeployedApps();

    /**
     * 根据路径（name） 获取对应的应用
     * @param contextPath
     * @return
     */
    Context findDeployedApp(String contextPath);

    /**
     * 下载应用
     * @param contextPath
     */
    void remove(String contextPath) throws IOException;
}
